package com.cpecc.manager.system;

import com.cpecc.dao.system.DepDao;
import com.cpecc.dao.system.UserDao;
import com.cpecc.model.system.Dep;
import com.cpecc.model.system.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class UserManager{
	
	@Autowired
	private UserDao userDao;

	@Autowired
	private DepDao depDao;

	public void addUser(User user) {
		userDao.addUser(user);
	}
	
	public void save(User user){
		userDao.save(user);
	}

	public List<User> getUsersInDep(String depID) {
		return userDao.getUsersInDep(depID);
	}

	public boolean delUser(String id) {
		return userDao.delUser(id);
	}

	public User getUser(String id) {
		return userDao.getUser(id);
	}

	public boolean updateUser(User user) {
		return userDao.updateUser(user);
	}

	public Set<String> getRoles(String username) {
		return userDao.getRoles(username);
	}

	public User findByUsername(String username) {
		return userDao.findByUsername(username);
	}
	
	public Dep getUserDep(User user){
		return userDao.getUserDep(user);
	}
	
	public boolean cover(User user){
		return userDao.cover(user);
	}
	
	public User getMiMaZH(String userName,String email){
		return userDao.getMiMaZH(userName, email);
	}
	
	public List<User> getUserByDep(String depId){
		return userDao.getUserByDep(depId);
	}

	public List<User> getUsersByGangWei(String gangWei) {
		return userDao.getUsersByGangWei(gangWei);
	}
	
	public boolean hasUser(Dep dep){
		return userDao.hasUser(dep);
	}
	
	public List<User> getBuMenLingDaos(User user){
		return userDao.getBuMenLingDaos(user);
	}
	
	public List<User> getBuMenLingDaos2(User user){
		return userDao.getBuMenLingDaos2(user);
	}
	
	public List<User> getAllCaiWuR(){
		return userDao.getAllCaiWuR();
	}
	
	public List<User> getCaiMaiR(){
		return userDao.getCaiMaiR();
	}
	
	public List<User> getGZGLY(){
		return userDao.getGZGLY();
	}
	
	public List<User> getCGBMFZR(){
		return userDao.getCGBMFZR();
	}
	
	public boolean isCGBMFZR(User user){
		return userDao.isCGBMFZR(user);
	}
	
	public List<User> getXinYongHu(){
		return userDao.getXinYongHu();
	}
	
	public void lock(User user){
		userDao.lock(user);
	}

	public User getBuMenFenGuanLingDao(String deptId) {
		Dep dep = depDao.get(deptId);
		if (dep == null) {
			return null;
		}

		User user = dep.getUser();
		if (user == null) {
			Dep parentDep = dep.getParent();
			if (parentDep != null) {
				user = parentDep.getUser();
			}
		}

		return user;
	}

	public boolean isGKBMFZR(User user) {
		List<User> gkbmfzrList = getGKBMFZR();
		for (User gkbmfzr : gkbmfzrList) {
			if (user.getId().equals(gkbmfzr.getId())) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 获取归口部门负责人
	 * @return
	 */
	public List<User> getGKBMFZR() {
		return userDao.getGKBMFZR();
	}

	/**
	 * 部门负责人
	 * @param user
	 * @return
	 */
	public List<User> getBMFZR(User user) {
		return userDao.getBMFZR(user);
	}
}
